/* -+- file  ./kf2/GUI/WelcomePage.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Mono.Unix;
using Habazie.kf2.Core;
using Habazie.kf2.Events;
using Habazie.kf2.Exposition;

namespace Habazie.kf2.GUI
{
	
	public class WelcomePage : Html
	{
		private EventBox m_EventBox = null;
		
		public WelcomePage() : base ()
		{
	//		OpenUri += OnOpenUri;
			PageTitle = "Welcome";
			Realized += OnR;
			AddHandler ("events/open", OnEventOpen);
			//Refresh ();
		//	LoadUrl (System.Environment.CurrentDirectory + "/welcome.html");
		}
		
		public void SetEventBox (EventBox box)
		{
			m_EventBox = box;
			m_EventBox.Updated += EventsUpdated;
		}
		
		
		public void Refresh ()
		{
			Clear ();
//			OpenStream ("file://" + System.Environment.CurrentDirectory+"/", "text/html");
//			AppendData ("<?xml version=\"1.0\" encoding=\"utf-8\"?><html><head><link rel=\"stylesheet\" href=\"default.css\"></head><body><h1>Welcome</h1>");
			
			AppendData (DrawBox ("Tab", "<a href=\"aaaa://blah?a=b&kekekek=erfer&bubu&trgtr=ert\">some info here</a>"));
			
			/* Events */
			if (m_EventBox != null) {
				System.Text.StringBuilder s = new System.Text.StringBuilder ("<ul>");
				foreach (Event e in m_EventBox) {
					s.Append ("<li class=\"event-");
					s.Append (e.EventName);
//					s.Append ("\"><a href=\"event:");
					s.Append ("\"><a href=\""+Html.EVENT_PROTOCOL+":events/open?id=");
					s.Append (e.Id);
					s.Append ("\">");
					s.Append (e.ToString ());
					s.Append ("</a></li>");					
				}
				s.Append ("</ul>");
				AppendData (DrawBox ("Events", s.ToString ()));
			}
			
			AppendData ("</body></html>");
			CloseStream ();
		}
		
		private string DrawBox (string title, string contents)
		{
			System.Text.StringBuilder s = new System.Text.StringBuilder ();
			s.Append (	"<div class=\"OuterBox\">"+
							"<h2>");
			s.Append (			title);
			s.Append (		"</h2>"+
							"<div class=\"InnerBox\">");
			s.Append (			contents);
			s.Append (		"</div>"+
						"</div>");
			return s.ToString ();
		}
		
		protected virtual void EventsUpdated (object sender, EventArgs e)
		{
			Refresh ();
		}
		
		protected virtual void OnEventOpen (object sender, HtmlEventArgs args)
		{
			try {
				int id = System.Int32.Parse (args["id"]);
				m_EventBox.Process (id);
			} catch (Exception e) {
				Util.ReportException (e);
			}
		}
		
		
		protected virtual void OnR (object sender, EventArgs e)
		{
//			Console.WriteLine ("dfesrgrtszre");
			Refresh ();
		}
		
	}
}
